local skel = fk.CreateSkill {
  name = "qf__youying",
}
Fk:loadTranslationTable{
  ["qf__youying"] = "游应",
  [":qf__youying"] = "你或你攻击范围内的角色使用伤害牌指定目标时，你可弃置一张同类型的牌并为之无视合法性地增加一个目标,若先/仅对该目标造成伤害，你可获得此伤害牌/使用者场上一张牌。",
}
skel:addEffect(fk.TargetSpecifying, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name)
    and data.card.is_damage_card and target and
    (target == player or player:inMyAttackRange(target))
    and not (data.extra_data and data.extra_data[skel.name] and data.extra_data[skel.name][player])
  end,
  on_cost = function (self, event, target, player, data)
    local targets = table.filter(player.room.alive_players, function (p)
      return not table.contains(data.use.tos, p)
    end)
    --data:getExtraTargets({bypass_distances = true})
    if #targets == 0 then return end
    local cards = player.room:askToDiscard(player, {
      min_num = 1, max_num = 1, include_equip = true, skill_name = skel.name, skip = true,
      pattern = ".|.|.|.|.|" .. data.card:getTypeString(),
      prompt = "游应：是否弃置一张同类型牌",
    })
    if #cards == 0 then return end
    local tos = player.room:askToChoosePlayers(player, {
      targets = targets, min_num = 1, max_num = 1, skill_name = skel.name,
      prompt = "选择一个额外目标"
    })
    if #tos == 0 then return end
    event:setCostData(self, {cards = cards, tos = tos})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local cost = event:getCostData(self)
    local room = player.room
    room:throwCard(cost.cards, skel.name, player, player)
    local to = cost.tos[1]
    data.extra_data = data.extra_data or {}
    data.extra_data[skel.name] = data.extra_data[skel.name] or {}
    data.extra_data[skel.name][player] = { to = to, record = {} }
    data:addTarget(to)
  end,
})

skel:addEffect(fk.Damage, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local useEvent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if not useEvent then return end
    local use = useEvent.data
    if use.card == data.card then
      return use.extra_data and use.extra_data[skel.name] and use.extra_data[skel.name][player]
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local useEvent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if not useEvent then return end
    local use = useEvent.data
    table.insert(use.extra_data[skel.name][player].record, data.to)
    if player.dead then return end
    local room = player.room
    local subcards = room:getSubcardsByRule(data.card, {Card.Processing})
    local ex_data = use.extra_data[skel.name][player]
    if #ex_data.record == 1 and ex_data.record[1] == ex_data.to then -- 首次伤害为指定目标
      room:obtainCard(player, subcards, true, fk.ReasonPrey, player, skel.name)
    end
  end,
})

skel:addEffect(fk.CardUseFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return data.extra_data and data.extra_data[skel.name] and data.extra_data[skel.name][player]
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local ex_data = data.extra_data[skel.name][player]
    if type(ex_data) ~= "table" then return end
    if #ex_data.record > 0 and table.every(ex_data.record, function (p)
      return p == ex_data.to
    end) then
      local from = data.from
      if player.dead or from.dead or #from:getCardIds("ej") == 0 then return end
      local cid = room:askToChooseCard(player, {
        target = from, flag = "ej", skill_name = skel.name,
      })
      room:obtainCard(player, cid, true, fk.ReasonPrey, player, skel.name)
    end
  end,
})



return skel